package com.ynet.middleground.riskmanage.service.impl;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.riskmanage.bean.PurchaseReq;
import com.ynet.middleground.riskmanage.dto.PurchaseInfoDTO;
import com.ynet.middleground.riskmanage.dto.PurchaseOrderDTO;
import com.ynet.middleground.riskmanage.dto.PurchaseReconciliationDTO;
import com.ynet.middleground.riskmanage.model.PurchaseCenterModel;
import com.ynet.middleground.riskmanage.service.PurchaseCenterService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName PurchaseCenterServiceImpl
 * @Description 对接采购中心服务实现类
 * @Author zhangyongbin
 * @Date 2020/8/13 14:50
 * @Version 1.0
 */
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "对接采购中心服务", tags = {"对接采购中心服务"})
@Path("purchase")
@Service(timeout = 6000, version = "0.0.1")
public class PurchaseCenterServiceImpl implements PurchaseCenterService, GenericService {

    @Autowired
    private PurchaseCenterModel purchaseCenterModel;

    @POST
    @Path("order")
    @ApiOperation(value = "获取订单信息服务", notes = "获取订单信息服务")
    @ApiImplicitParam(name = "requestObj", dataType = "PurchaseOrderDataReq", paramType = "body", required = true)
    @Override
    public ServiceResult<PurchaseOrderDTO> getOrderData(@Valid PurchaseReq req) {
        ServiceResult<PurchaseOrderDTO> result = new ServiceResult<>();
        try {
            result.setResult(purchaseCenterModel.getOrderData(req));
            result.setPagerInfo(req.getPagerInfo());
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "获取订单信息服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;

    }

    @POST
    @Path("invoice")
    @ApiOperation(value = "获取发票信息服务", notes = "获取发票信息服务")
    @ApiImplicitParam(name = "requestObj", dataType = "PurchaseOrderDataReq", paramType = "body", required = true)
    @Override
    public ServiceResult<PurchaseInfoDTO> getInvoiceData(@Valid PurchaseReq req) {
        ServiceResult<PurchaseInfoDTO> result = new ServiceResult();
        try {
            result.setResult(purchaseCenterModel.getAllData(req));
            result.setPagerInfo(req.getPagerInfo());
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "获取发票信息服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

    /**
     * 查询对账单信息
     *
     * @param req 获取订单、对账单数据请求信息
     * @return 对账单信息列表
     * @author liulx
     * @date 2020-08-13 20:41:55
     */
    @POST
    @Path("reconciliation")
    @ApiOperation(value = "获取对账单服务", notes = "获取对账单服务")
    @ApiImplicitParam(name = "requestObj", dataType = "PurchaseReq", paramType = "body", required = true)
    @Override
    public ServiceResult<PurchaseReconciliationDTO> listReconciliation(@Valid PurchaseReq req) {
        ServiceResult<PurchaseReconciliationDTO> result = new ServiceResult<>();
        try {
            result.setResult(purchaseCenterModel.listReconciliation(req));
            result.setPagerInfo(req.getPagerInfo());
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "获取对账单服务异常, case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECRM0000", "服务异常，请联系系统管理员");
        }
        return result;
    }

}
